var gulp = require('gulp');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var clean = require('gulp-clean'); //清空文件夹
var rev = require('gulp-rev'); //更改版本号
var revReplace = require('gulp-rev-replace');
var htmlmin = require('gulp-htmlmin');
var nghtml2js = require('gulp-ng-html2js');//html转js

var paths = {
  js: [
    "../www/js/framework/app.js",
    "../www/js/framework/AppRun.js",
    "../www/js/framework/DataSave.js",
    "../www/js/framework/UrlService.js",
    "../www/js/framework/UserService.js",
    "../www/js/framework/Key.js",
    "../www/js/framework/Filters.js",
    "../www/js/framework/PopupService.js",
    "../www/js/framework/ionicConfig/ConfigCommon.js",
    "../www/js/framework/ionicConfig/ConfigInterceptors.js",
    "../www/js/framework/ionicConfig/ConfigRoute.js",
    "../www/js/framework/ionicConfig/IonicConfig.js",
    "../www/js/framework/FixScrollService.js",
    '../www/js/**/*.js'],
  templates: ['../www/templates/**/*.html',
    '../www/templates/**/**/*.html'],
  lib: ['../www/lib/**/*.min.js',
    '../www/lib/**/*.eot',
    '../www/lib/**/*.svg',
    '../www/lib/**/*.ttf',
    '../www/lib/**/*.woff'],
  img: ['../www/img/**/*.*'],
  iconfont: ['../www/css/**/*.*'],
  devcode: ['../www/css',
    '../www/img',
    '../www/js',
    '../www/lib',
    '../www/templates',
    '../www/index.html']
};


/**
 * 清理不必要的文件
 * ionic.app.css
 * style.css
 */
gulp.task('clean', function (done) {
  return gulp.src(['../www/release/*'], {read: false}) //{read:false}，是不读取文件加快程序。
    .pipe(clean({force: true}))
});

gulp.task('lib', ['clean'], function (done) {
  return gulp.src(paths.lib)
    .pipe(gulp.dest('../www/release/lib/'))
});
gulp.task('img', ['lib'], function (done) {
  return gulp.src(paths.img)
    .pipe(gulp.dest('../www/release/img/'))
});

gulp.task('css', ['img'], function (done) {
    return gulp.src('../www/css/ionic.app.min.css')
      .pipe(rev())
      .pipe(gulp.dest('../www/release/'))
      .pipe(rev.manifest())
      .pipe(gulp.dest('./'))
  }
);

gulp.task('js', ['css'], function (done) {
  return gulp.src(paths.js)
    .pipe(concat('framework.js'))
    .pipe(rev())
    .pipe(gulp.dest('../www/release/'))
    /*.pipe(rename({suffix: '.min'}))
     .pipe(uglify())*/
    .pipe(rev.manifest({merge: true}))
    .pipe(gulp.dest('./'));
  /*执行下一个任务*/
});

gulp.task('html', ['js'], function (done) {
  return gulp.src(paths.templates)//读取templates文件

    /*压缩html*/
    .pipe(htmlmin({collapseWhitespace: true}))

    /*将html转换为js*/
    .pipe(nghtml2js({
      moduleName: 'app.templates',
      prefix: 'templates/'
    }))

    /*合并到templates.js文件中*/
    .pipe(concat('templates.js'))

    /*混淆文件*/
    // .pipe(uglify())

    .pipe(rev())
    /*将合并后的代码输出到/www/js目录下*/
    .pipe(gulp.dest('../www/release/'))

    .pipe(rev.manifest({merge: true}))
    .pipe(gulp.dest('./'))

});

gulp.task('default', ["html"], function (done) {
  var manifest = gulp.src('./rev-manifest.json');
  gulp.src('./data/index.html') //该任务针对的文件
    .pipe(revReplace({manifest: manifest}))
    .pipe(gulp.dest('../www/release/'))
    .on('end', done);
});


gulp.task('clear-all', function (done) {
  return gulp.src(paths.devcode, {read: false}) //{read:false}，是不读取文件加快程序。
    .pipe(clean({force: true}))
});
gulp.task('copy', ['clear-all'], function (done) {
  return gulp.src(['../www/release/**/*.*'])
    .pipe(gulp.dest('../www/'))
});

gulp.task('release', ['copy'], function (done) {
  gulp.src(['../www/release'])
    .pipe(clean({force: true}))
    .on('end', done);
});

